//
//  CommentCell.m
//  quickLook
//
//  Created by J on 15/9/1.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "CommentCell.h"
#import "Header.h"
#import "CommentController.h"
@interface CommentCell ()
{

    NSIndexPath* _indexPath;
    BOOL _isExisted;
    BOOL _isSelected;


}
@property (weak, nonatomic) IBOutlet UIImageView *avatar_url;

@property (weak, nonatomic) IBOutlet UILabel *nickname;

@property (weak, nonatomic) IBOutlet UILabel *created_at;

@property (weak, nonatomic) IBOutlet UILabel *content;
@property (weak, nonatomic) IBOutlet UIImageView *goodHand;
@property (weak, nonatomic) IBOutlet UILabel *likes_count;
@end

@implementation CommentCell

- (void)awakeFromNib {
    // Initialization code
    
    _avatar_url.layer.cornerRadius=25;
    
    _avatar_url.layer.masksToBounds = YES;
    
    
  
    
    UITapGestureRecognizer* goodTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goodTap:)];
    
    
    _goodHand.userInteractionEnabled=YES;
    [_goodHand addGestureRecognizer:goodTap];
    
    
    
    
    

    
    
    

}


- (void)goodTap:(UITapGestureRecognizer*)tap{
    
    
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
 
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];

    
    
    
    Model* model= self.delegate.dataArray[_indexPath.row];
    
    
    CommentCell* cell=[self.delegate.tableView cellForRowAtIndexPath:_indexPath];

    
      
    [UIView animateWithDuration:0.3 animations:^{
        
        _goodHand.transform= CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0);
        
        if (_isSelected==NO) {
            
            _isSelected=YES;
            _likes_count.textColor=[UIColor colorWithRed:135/255.0 green:135/255.0  blue:135/255.0  alpha:1.0];
            
            
            _likes_count.textColor=JZJYELLOW;
            
            
            _goodHand.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];
            
            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            [mutaArray addObjectsFromArray:mutableArray];
            
            

            if (_isExisted==NO) {
                [mutaArray addObject:model.id];
                [userdefault setObject:mutaArray forKey:IDArray];
                [userdefault synchronize];
                _isExisted=YES;
                
                
            }

            cell.likes_count.text=[NSString stringWithFormat:@"%zd",[cell.likes_count.text integerValue]+1];

            
        }else{

            _isSelected=NO;

            _goodHand.image=[UIImage imageNamed:@"quickLook2.png"];
            _likes_count.textColor=TEXTCOLOR;
            
            
            
            NSMutableArray *mutaArray = [[NSMutableArray alloc] init];
            [mutaArray addObjectsFromArray:mutableArray];
            [mutaArray removeObject:model.id];
            
            [userdefault setObject:mutaArray forKey:IDArray];
            
            
            [userdefault synchronize];
            cell.likes_count.text=[NSString stringWithFormat:@"%zd",[cell.likes_count.text integerValue]-1];


            
        }
        
        
        
        
        
        
        
        
    } completion:^(BOOL finished) {
        
        
        [UIView animateWithDuration:0.3 animations:^{
            _goodHand.transform= CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
            
        }];
        
    }];
    
    

//    JZJLog(@"comment::%@",[userdefault objectForKey:IDArray]);
    
}

- (IBAction)isLiked:(id)sender {
    
    
    
    
    
    
}


- (void)refreshWithModel:(Model*)model WithIndexPath:(NSIndexPath*)path
{

    
    _indexPath=path;
    _goodHand.tag=path.row;
    
    
    

    
    
    _likes_count.text=[NSString stringWithFormat:@"%@",model.likes_count];
    
    [_avatar_url sd_setImageWithURL:[NSURL URLWithString:model.user[@"avatar_url"]]];
    
    
    
    
    _nickname.text=[model.user objectForKey:@"nickname"];
    
    //    JZJLog(@"%@",_nickname.text);
    
    _created_at.text=[self createAt:model];
    
    
    _content.text=model.content;
    
    [self setIntroductionText:_content.text];

    
    
    
    
    //查找id
    
    NSUserDefaults* userdefault=[NSUserDefaults standardUserDefaults];
    
    
    NSMutableArray* mutableArray=[userdefault objectForKey:IDArray];
    

    

    
    
    [mutableArray enumerateObjectsUsingBlock:^(NSNumber* obj, NSUInteger idx, BOOL *stop) {
        
        if ([model.id isEqualToNumber:obj]) {
            _isExisted=YES;
            _isSelected=YES;
            
            _likes_count.text=[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[_likes_count.text integerValue]+1]];
            _goodHand.image=[UIImage imageNamed:@"quickLook2Hilighted.png"];

        }else
        {
            _isExisted=NO;
            _isSelected=NO;
//            model.likes_count=[NSNumber numberWithInteger:[model.likes_count integerValue]-1];
            _goodHand.image=[UIImage imageNamed:@"quickLook2.png"];

        
        
        
        }
    }];
    
    
   
    

   
}


- (NSString*)subTimeWithStr:(NSString*)dateStr{
    
    NSDateFormatter* df=[[NSDateFormatter alloc]init];//时间戳 转化NSString和NSDate
    
    //设置格式
    df.dateFormat=@"yyyy-MM-dd HH:mm:ss.0";//大M代表月 m代表分钟
    
    //将符合格式的字符串转成NSDate对象
    NSDate* date=[df dateFromString:dateStr];
    
    //计算一个时间与当前时间差
    int second=  [date timeIntervalSinceNow];//typedef double NSTimeInterval;
    
    
    //7300  : :40
    return [NSString stringWithFormat:@"剩余: %02d:%02d:%02d",second/3600,second%3600/60,second%60];
    
    
    
    
}





- (NSString*)createAt:(Model*)model{

    NSString *str=[NSString stringWithFormat:@"%@",model.created_at];//时间戳
    NSTimeInterval time=[str doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
//    JZJLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
   

    return currentDateStr;

}


-(void)setIntroductionText:(NSString*)text{
    
    //获得当前cell高度
    
    CGRect frame = [self frame];
    
    //文本赋值
    
    self.content.text = text;
    
    //设置label的最大行数
    
    self.content.numberOfLines = 50;
    CGSize size = CGSizeMake(300, 1000);
    CGSize labelSize = [self.content.text sizeWithFont:self.content.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
//    NSMutableParagraphStyle* pragraphStyle = [NSMutableParagraphStyle new];
//    pragraphStyle.lineBreakMode = NSLineBreakByClipping;
//    NSDictionary* attributes  = @{NSFontAttributeName:self.textLabel.font,NSParagraphStyleAttributeName:pragraphStyle.copy};
//    
//    CGSize labelSize = [self.content.text boundingRectWithSize:CGSizeMake(SCROLLWIDTH, 999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

    
    self.content.frame = CGRectMake(self.content.frame.origin.x, self.content.frame.origin.y, labelSize.width, labelSize.height);
    
    
    
    //计算出自适应的高度
    
    frame.size.height = labelSize.height+100;
    
    
    
    self.frame = frame;  
    
}

-(void)layoutSubviews
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
